package com.lottery.material.service.admin.promotion.impl;

import com.lottery.material.dao.promotion.PromotionRuleConfigMapper;
import com.lottery.material.enumeration.common.CommonEnum;
import com.lottery.material.enumeration.promotion.PromotionRuleConfigEnum;
import com.lottery.material.pojo.page.Page;
import com.lottery.material.pojo.promotion.PromotionRuleConfig;
import com.lottery.material.pojocustom.promotion.PromotionRuleConfigCustom;
import com.lottery.material.service.admin.promotion.PromotionRuleConfigService;
import com.lottery.material.util.CalendarUtil;
import com.lottery.material.util.EnumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Description：推广规则配置服务接口实现类
 * Created by 朱正磊 on 2019-04-13 17:57
 */
@Service("adminPromotionRuleConfigService")
public class PromotionRuleConfigServiceImpl implements PromotionRuleConfigService {

    private final PromotionRuleConfigMapper promotionRuleConfigMapper;

    @Autowired
    public PromotionRuleConfigServiceImpl(PromotionRuleConfigMapper promotionRuleConfigMapper) {
        this.promotionRuleConfigMapper = promotionRuleConfigMapper;
    }

    /**
     * description：新增一条数据
     * user 朱正磊
     * time 2019-04-20 15:14
     * @param promotionRuleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer add(PromotionRuleConfig promotionRuleConfig) {
        promotionRuleConfig.setCreateTime(new Date());
        promotionRuleConfig.setIsDelete((Integer) CommonEnum.IS_DELETE_FALSE.getValue()); //默认未逻辑删除
        Integer promotionDuration = promotionRuleConfig.getPromotionDuration(); //小时
        promotionDuration = promotionDuration * 3600; //秒
        promotionRuleConfig.setPromotionDuration(promotionDuration);
        return promotionRuleConfigMapper.insertSelective(promotionRuleConfig);
    }

    /**
     * description：根据主键物理删除
     * user 朱正磊
     * time 2019-04-20 15:13
     * @param id 主键
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer deleteById(Integer id) {
        return promotionRuleConfigMapper.deleteByPrimaryKey(id);
    }

    /**
     * description：根据主键查询唯一数据
     * user 朱正磊
     * time 2019-04-20 15:15
     * @param id 主键
     * @return 返回唯一数据
     */
    @Override
    public PromotionRuleConfig findById(Integer id) {
        return promotionRuleConfigMapper.selectByPrimaryKey(id);
    }

    /**
     * description：根据主键修改一条数据
     * user 朱正磊
     * time 2019-04-20 15:16
     * @param promotionRuleConfig 对象
     * @return 返回持久化操作数据库状态值
     */
    @Override
    public Integer updateById(PromotionRuleConfig promotionRuleConfig) {
        Integer promotionDuration = promotionRuleConfig.getPromotionDuration(); //小时
        promotionDuration = promotionDuration * 3600; //秒
        promotionRuleConfig.setPromotionDuration(promotionDuration);
        return promotionRuleConfigMapper.updateByPrimaryKeySelective(promotionRuleConfig);
    }

    /**
     * description：查询扩展类分页列表
     * user 朱正磊
     * time 2019-04-17 17:01
     * @param page 分页对象
     * @return 返回扩展类分页列表数据
     */
    @Override
    public List<PromotionRuleConfigCustom> findCustomListPage(Page page) {
        List<PromotionRuleConfigCustom> list = promotionRuleConfigMapper.findCustomListPage(page);
        list.forEach(this::setCustom);
        return list;
    }

    /**
     * description：根据主键查询唯一扩展类数据
     * user 朱正磊
     * time 2019-04-17 17:03
     * @param id 主键
     * @return 返回唯一扩展类数据
     */
    @Override
    public PromotionRuleConfigCustom findCustomById(Integer id) {
        PromotionRuleConfigCustom custom = promotionRuleConfigMapper.findCustomById(id);
        this.setCustom(custom);
        return custom;
    }

    /**
     * description：设置扩展类的扩展属性
     * user 朱正磊
     * time 2019-04-17 17:48
     * @param custom 扩展类对象
     */
    private void setCustom(PromotionRuleConfigCustom custom) {
        //创建时间
        custom.setCreateTimeFormat(CalendarUtil.dateToString(custom.getCreateTime()));
        //是否删除（1：否；2：是）
        custom.setIsDeleteDesc(EnumUtil.getDescriptionByCodeValue(CommonEnum.IS_DELETE,
                custom.getIsDelete(), CommonEnum.class));
        //推广类型（1：圈子内文章推广；2：群聊内文章推广）
        custom.setPromotionTypeDesc(EnumUtil.getDescriptionByCodeValue(PromotionRuleConfigEnum.PROMOTION_TYPE,
                custom.getPromotionType(), PromotionRuleConfigEnum.class));
        //推广时长描述（小时）
        Integer hours = custom.getPromotionDuration() / 3600;
        custom.setPromotionDurationDesc(hours.toString());
    }

    /**
     * description：根据推广类型查询唯一数据（一个推广类型只对应一条数据）
     * user 朱正磊
     * time 2019-04-30 14:47
     * @param type 推广类型
     * @return 返回唯一数据
     */
    @Override
    public PromotionRuleConfig findByType(Integer type) {
        return promotionRuleConfigMapper.findByType(type);
    }

    /**
     * description：查询所有的数据（当然不包括已经逻辑删除的数据，其实该业务没有删除操作）
     * user 朱正磊
     * time 2019-05-15 15:13
     * @return 返回所有的集合列表数据
     */
    @Override
    public List<PromotionRuleConfigCustom> findAll() {
        List<PromotionRuleConfigCustom> list = promotionRuleConfigMapper.findAll();
        list.forEach(this::setCustom);
        return list;
    }

}
